package com.stylefeng.guns.biz.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.biz.api.service.IMiniService;
import com.stylefeng.guns.biz.api.task.TaskFactory;
import com.stylefeng.guns.biz.app.model.TAppList;
import com.stylefeng.guns.biz.app.service.ITAppListService;
import com.stylefeng.guns.biz.banner.model.TBanner;
import com.stylefeng.guns.biz.business.model.TBusiness;
import com.stylefeng.guns.biz.hot.model.HotList;
import com.stylefeng.guns.biz.nav.model.TNavList;
import com.stylefeng.guns.biz.page.model.TPageList;
import com.stylefeng.guns.biz.recommend.model.Recommend;
import com.stylefeng.guns.biz.share.model.Share;
import com.stylefeng.guns.biz.share.service.IShareService;
import com.stylefeng.guns.biz.singlePage.model.TSinglePage;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.result.BaseResultVo;
import com.stylefeng.guns.core.result.JsonReturnHelper;
import com.stylefeng.guns.modular.system.model.BusinessClassify;
import com.stylefeng.guns.modular.system.model.Dict;

/**
 * 内容管理控制器
 *
 * @author sundm
 * @Date 2018-06-14 06:33:39
 */
@Controller
@RequestMapping("/api/mini")
public class MiniController extends BaseController {


	@Autowired
	private ITAppListService appListService;
	@Autowired
	private IMiniService miniService;
	@Autowired
	private IShareService shareService;
	
    /**
     * 获取首页
     */
    @RequestMapping(value = "/getIndex")
    @ResponseBody
    public BaseResultVo getIndex(String appid) {
    	
    	EntityWrapper<TAppList> wrapper=new EntityWrapper<>();
    	wrapper.eq("app_id", appid);
    	
    	TAppList appList = appListService.selectOne(wrapper);
    	List<Dict>  list = ConstantFactory.me().findInDict(45);
    	for (Dict dict : list) {
    		if(appList.getPageType().toString().equals(dict.getNum().toString())){
    			appList.setUrl(dict.getName());
    			break;
    		}
		}
    	
        return JsonReturnHelper.getSuccessWithData(appList);
    }
    
    /**
     * 获取首页数据
     */
    @RequestMapping(value = "/getMainPage")
    @ResponseBody
    public BaseResultVo getMainPage(String appid) {
    	
    	List<TBanner> banners = miniService.getBanners(appid,1);

//    	List<TTableList> tableLists = miniService.getTables(appid);

    	List<BusinessClassify> classifys = miniService.getBusinessClassifys(appid);
    	for (BusinessClassify businessClassify : classifys) {
    		List<TBusiness> businesses = miniService.getBusiness(appid,businessClassify.getId());
    		businessClassify.setBusiness(businesses);
		}

    	//hot list
    	List<HotList> hotLists = miniService.getHots(appid);
    	
    	Map<String, Object> map=new HashMap<>();
    	
    	map.put("banners", banners);
//    	map.put("tableLists", tableLists);
    	map.put("classifys", classifys);
//    	map.put("navLists", navLists);
    	map.put("hotLists", hotLists);
    	
        return JsonReturnHelper.getSuccessWithData(map);
    }
    
    /**
     * 获取banner
     * @param appid
     * @return
     */
    @RequestMapping(value = "/getBanners")
    @ResponseBody
    public BaseResultVo getBanners(String appid,Integer type) {

    	List<TBanner> banners = miniService.getBanners(appid,type);
    	 
    	return JsonReturnHelper.getSuccessWithData(banners);
    }
    
    /**
     * 多页面
     */
    @RequestMapping(value = "/getPageById")
    @ResponseBody
    public BaseResultVo getPageById(Long pageId) {

    	TPageList page = miniService.getPage(pageId);
    	
    	return JsonReturnHelper.getSuccessWithData(page);
    
    }
    
    /**
     * 获取单页面
     */
    @RequestMapping(value = "/getSinglePage")
    @ResponseBody
    public BaseResultVo getSinglePage(String appid) {
    	
    	TSinglePage page = miniService.getSinglePage(appid);
    	
    	return JsonReturnHelper.getSuccessWithData(page);
    }
    
    /**
     * 获取单页面
     */
    @RequestMapping(value = "/getClickData")
    @ResponseBody
    public BaseResultVo getClickData(String appid,String source,Integer jumpType,String customerAppId) {
    	
    	//保存点击数据
    	LogManager.me().executeLog(TaskFactory.saveClickData(appid, source, jumpType,customerAppId));
    	
    	return JsonReturnHelper.getSuccessWithNull();
    }
    
    
    /**
     * 获取分享数据
     */
    @RequestMapping(value = "/getShareSetting")
    @ResponseBody
    public BaseResultVo getShareSetting(String appid,String type) {
    	
    	EntityWrapper<Share> wrapper=new EntityWrapper<>();
    	wrapper.eq("app_id_key", appid);
    	wrapper.eq("share_page_type", type);
    	wrapper.eq("is_open", 1);
    	Share rs =shareService.selectOne(wrapper);
    	return JsonReturnHelper.getSuccessWithData(rs);
    }
    
    
    /**
     * 获取导航列表
     */
    @RequestMapping(value = "/getNavlist")
    @ResponseBody
    public BaseResultVo getNavlist(String appid) {
    	
    	List<TNavList> navLists = miniService.getNavs(appid);
    	
    	return JsonReturnHelper.getSuccessWithData(navLists);
    }
    
    /**
     * 获取今日推荐列表
     */
    @RequestMapping(value = "/getRecommends")
    @ResponseBody
    public BaseResultVo getRecommends(String appid) {
    	
    	List<Recommend> res = miniService.getRecommends(appid);
    	
    	return JsonReturnHelper.getSuccessWithData(res);
    }
    


}
